<template>
  <div class="detail">
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录帐户设置">
          <el-form
            ref="form"
            label-width="100px"
            class="form"
            :model="form"
            :rules="rules"
          >
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" show-password />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" lazy>
          <Info :user-info="userInfo1" @getData="getData" />
        </el-tab-pane>
        <el-tab-pane label="岗位信息">
          <job />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import { sysUser, sysUserPut } from '@/api/login'
export default {
  components: {
    Info: () => import('./Info.vue'),
    job: () => import('./Job.vue')
  },
  data() {
    return {
      id: this.$route.params.id,
      form: {
        username: '', //  string  非必须
        password: '' //  string  非必须
      },
      userInfo: {},
      userInfo1: {},
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }], //  string  非必须
        password: [{ required: true, message: '必填', trigger: 'change' }] //  string  非必须
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.id)
      // 只需要回显用户名就可以了
      this.userInfo = res.data
      this.userInfo1 = { ...res.data }
      this.form.username = res.data.username
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPut({ ...this.userInfo, ...this.form })
          this.$message.success('更新成功')

          if (this.$store.state.user.userInfo.id === this.id) {
            this.$store.dispatch('user/getUserInfo')
          }
          this.getData()
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.detail {
  padding: 20px;
  .form {
    width: 500px;
    margin-top: 50px;
  }
}
</style>
